//
//  SEGNewAlertView.m
//  ExcellentHome
//
//  Created by 揭婧雯 on 2017/8/23.
//  Copyright © 2017年 陈区. All rights reserved.
//

#import "SEGNewAlertView.h"

#if __has_include(<Commom/SEGColorManager.h>)
#import <Commom/SEGColorManager.h>
#else
#import "SEGColorManager.h"
#endif

#if __has_include(<Commom/SEGLayoutMacro.h>)
#import <Commom/SEGLayoutMacro.h>
#else
#import "SEGLayoutMacro.h"
#endif

#import <Masonry/Masonry.h>

#import "SEGPlatformHeader.h"

@interface SEGNewAlertView ()
@property (nonatomic, strong) UIView *contentView;
@property (nonatomic, copy)   SEGAlertSelectIndexBlock selectBlock;
@end

@implementation SEGNewAlertView

- (void)showWithAlertText:(NSString *_Nullable)text delegate:(id _Nullable)delegate buttonTitlesArray:(NSArray <NSString *>*_Nullable)buttonTitlesArray
{
    [self showWithAlertTitle:nil text:text delegate:delegate buttonTitlesArray:buttonTitlesArray];
}

- (void)showWithAlertTitle:(NSString *)title text:(NSString *)text delegate:(id)delegate buttonTitlesArray:(NSArray<NSString *> *)buttonTitlesArray
{
    self.delegate = delegate;
    
    [self initSubViewWithAlertTitle:title text:text buttonTitlesArray:buttonTitlesArray];
}

- (void)showWithAlertTitle:(NSString *)title text:(NSString *)text buttonTitlesArray:(NSArray<NSString *> *)buttonTitlesArray actionBlock:(SEGAlertSelectIndexBlock)block {
    self.selectBlock = block;
    [self initSubViewWithAlertTitle:title text:text buttonTitlesArray:buttonTitlesArray];
}

- (void)initSubViewWithAlertTitle:(NSString *)title text:(NSString *)text buttonTitlesArray:(NSArray<NSString *> *)buttonTitlesArray {
    self.frame = CGRectMake(0, 0, LYScreenWidth(), LYScreenHeight());
    [[[UIApplication sharedApplication] keyWindow] addSubview:self];
    self.alpha = 0;
    self.backgroundColor = kColor_bg3;
    
    //白色背景视图
    UIView *contentView = [[UIView alloc] init];
    contentView.backgroundColor = kColor_W;
    contentView.layer.masksToBounds = YES;
    contentView.layer.cornerRadius = 5;
    self.contentView = contentView;
    [self addSubview:contentView];
    [contentView mas_makeConstraints:^(MASConstraintMaker* make) {
        make.centerX.equalTo(self.mas_centerX);
        make.centerY.equalTo(self.mas_centerY);
        make.width.mas_equalTo(270.0);
    }];
    
    //内容
    UILabel *contentLabel = [[UILabel alloc] init];
    contentLabel.textColor = kColor_1;
    contentLabel.backgroundColor = [UIColor clearColor];
    contentLabel.numberOfLines = 0;
    if (self.labelLeft) {
        contentLabel.textAlignment = NSTextAlignmentLeft;
    }else{
        contentLabel.textAlignment = NSTextAlignmentCenter;
    }
    [contentView addSubview:contentLabel];
    contentLabel.text = text;
    
    CGFloat buttonTopPadding;
    if (title && title.length > 0) {
        //标题
        UILabel *titleLabel = [[UILabel alloc] init];
        titleLabel.textColor = kColor_1;
        titleLabel.font = [UIFont boldSystemFontOfSize:16];
        titleLabel.backgroundColor = [UIColor clearColor];
        titleLabel.numberOfLines = 0;
        titleLabel.textAlignment = NSTextAlignmentCenter;
        [contentView addSubview:titleLabel];
        titleLabel.text = title;
        [titleLabel mas_makeConstraints:^(MASConstraintMaker* make) {
            make.centerX.equalTo(contentView.mas_centerX);
            make.top.equalTo(contentView).offset(19);
            make.left.equalTo(contentView).offset(15);
            make.right.equalTo(contentView).offset(-15);
        }];
        
        contentLabel.font = kFont_13;
        [contentLabel mas_makeConstraints:^(MASConstraintMaker* make) {
            make.centerX.equalTo(contentView.mas_centerX);
            make.top.equalTo(titleLabel.mas_bottom).offset(14);
            make.left.equalTo(contentView).offset(15);
            make.right.equalTo(contentView).offset(-15);
        }];
        
        buttonTopPadding = 19;
    }else {
        contentLabel.font = kFont_16;
        [contentLabel mas_makeConstraints:^(MASConstraintMaker* make) {
            make.centerX.equalTo(contentView.mas_centerX);
            make.top.equalTo(contentView).offset(29);
            make.left.equalTo(contentView).offset(15);
            make.right.equalTo(contentView).offset(-15);
        }];
        
        buttonTopPadding = 29;
    }
    
    NSInteger totalCount = buttonTitlesArray.count;
    CGFloat buttonWidth = 270.0 / totalCount;////根据按钮个数平分
    
    [buttonTitlesArray enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        //按钮
        UIButton *button = [[UIButton alloc] init];
        button.backgroundColor = [UIColor clearColor];
        [button setTitle:obj forState:UIControlStateNormal];
        //        [button setTitleColor:kColor_1 forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:16];
        button.layer.borderWidth = 0.5;
        button.layer.borderColor = kColor_L2.CGColor;
        
        button.tag = idx;
        [button addTarget:self action:@selector(seg_buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [contentView addSubview:button];
        [button mas_makeConstraints:^(MASConstraintMaker* make) {
            make.top.equalTo(contentLabel.mas_bottom).offset(buttonTopPadding);
            make.left.equalTo(contentView.mas_left).offset(idx * buttonWidth);
            make.width.mas_equalTo(buttonWidth);
            make.height.mas_equalTo(44);
            make.bottom.mas_equalTo(contentView);
        }];
        if (self.nodefaultColor) {
            [button setTitleColor:self.nodefaultColor forState:UIControlStateNormal];
        }else{
            if (button.tag == totalCount - 1) {//最后一个按钮
                [button setTitleColor:kColor_T forState:UIControlStateNormal];
            }else {
                [button setTitleColor:kColor_1 forState:UIControlStateNormal];
            }
            
        }
    }];
    
    [self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissWhenTouchAction)]];
    //防止点击白色的背景view 消失
    [contentView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(nothingAction)]];
    [UIView animateWithDuration:0.2 animations:^{
        self.alpha = 1;
    }];
}

- (void)seg_buttonClick:(UIButton*)sender
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(segNewAlertView:clickedButtonAtIndex:)]) {
        [self.delegate segNewAlertView:self clickedButtonAtIndex:sender.tag];
    }
    if (self.selectBlock) {
        self.selectBlock(sender.tag);
    }
    [self dissmissAction];
}

- (void)dismissWhenTouchAction {
    if (self.dismissWhenTouch) {
        [self dissmissAction];
    }
}

- (void)dissmissAction {
    [UIView animateWithDuration:0.2 animations:^{
        self.alpha = 0;
    }completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

//防止点击白色的背景view 消失
- (void)nothingAction {
    
}

- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection {
    [super traitCollectionDidChange:previousTraitCollection];
    for (UIView *view in self.contentView.subviews) {
        if ([view isKindOfClass:[UIButton class]]) {
            if (view.layer.borderWidth > 0.01) {
                view.layer.borderColor = kColor_L2.CGColor;
            }
        }
    }
}
@end
